package com.gy.mbaselibrary.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.gy.mbaselibrary.R;

/**
 * 创建者：高业
 * 邮箱：15234150168@163.com
 * 时间：2018/2/6
 * 修改人：
 * 修改时间：2018/2/6
 * 修改备注：
 */
public class NetDialog extends Dialog {
    private Animation animation;
    private ImageView iv_net;
    private TextView tv_text;


    public NetDialog(Context context) {
        super(context);
        init();
    }

    public NetDialog(Context context, int themeResId) {
        super(context, themeResId);
        init();
    }

    protected NetDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init();
    }

    private void init() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_base_net,null);
        setContentView(view);
        iv_net = view.findViewById(R.id.iv_net);
        tv_text = view.findViewById(R.id.tv_text);
        setCancelable(true);//点击外部不可dismiss
        setCanceledOnTouchOutside(true);
        Window window = this.getWindow();
        window.setGravity(Gravity.CENTER);
        //window.setBackgroundDrawable(null);
        window.setDimAmount(0.5f);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //params.format = PixelFormat.TRANSLUCENT;
        //params.alpha = 0.5f;
        window.setAttributes(params);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                iv_net.clearAnimation();
                animation = null;
            }
        });
    }

    @Override
    public void show() {
        if (!isShowing()){
            super.show();
            animation = AnimationUtils.loadAnimation(getContext(),R.anim.rotate_base);
            iv_net.setAnimation(animation);
        }
        tv_text.setVisibility(View.GONE);

    }

    public void show(String str){
        if (!isShowing()){
            super.show();
            animation = AnimationUtils.loadAnimation(getContext(),R.anim.rotate_base);
            iv_net.setAnimation(animation);
        }
        tv_text.setVisibility(View.VISIBLE);
        tv_text.setText(str);
    }


}
